/**
 * 
 */
package com.pmsco.pms.business;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pmsco.pms.business.dto.MedicineRequestDTO;
import com.pmsco.pms.business.dto.MedicineReturnDTO;
import com.pmsco.pms.core.constant.PmsConstant;
import com.pmsco.pms.core.domain.Category;
import com.pmsco.pms.core.domain.Medicine;
import com.pmsco.pms.core.domain.MedicineCategory;
import com.pmsco.pms.services.impl.CategoryServiceImpl;
import com.pmsco.pms.services.impl.MedicineCategoryServiceImpl;
import com.pmsco.pms.services.impl.MedicineServiceImpl;
import com.pmsco.pms.services.impl.SubCategoryServiceImpl;

/**
 * @author Long Tran
 *
 */
public class MedicineBusiness {
	private static final Logger logger = LoggerFactory.getLogger(MedicineBusiness.class);
	private MedicineServiceImpl medicineService = new MedicineServiceImpl();
	private CategoryServiceImpl categoryService = new CategoryServiceImpl();
	private SubCategoryServiceImpl subCategoryService = new SubCategoryServiceImpl();
	private MedicineCategoryServiceImpl medicineCategoryService = new MedicineCategoryServiceImpl();
	
	public MedicineReturnDTO listMedicineData(MedicineRequestDTO medicineRequestDTO) {
		logger.info("Start listMedicineData");
		MedicineReturnDTO medicineReturnDTO = new MedicineReturnDTO();
		
		List<Medicine> medicines = medicineService.listMedicine(medicineRequestDTO.getSearchField(), 
				medicineRequestDTO.getSearchString(), 
				medicineRequestDTO.getSearchOper(), 
				medicineRequestDTO.getSidx(), 
				medicineRequestDTO.getSord(), 
				medicineRequestDTO.getPage(), 
				medicineRequestDTO.getRows());
		Integer medicinesSize = medicineService.countListMedicine(medicineRequestDTO.getSearchField(), 
				medicineRequestDTO.getSearchString(), 
				medicineRequestDTO.getSearchOper());
		
		medicineReturnDTO.setMedicines(medicines);
		medicineReturnDTO.setMedicinesSize(medicinesSize);
		
		logger.info("End listMedicineData");
		return medicineReturnDTO;
	}
	
	public MedicineReturnDTO editMedicine(MedicineRequestDTO medicineRequestDTO) {
		logger.info("Start editMedicine");
		MedicineReturnDTO medicineReturnDTO = new MedicineReturnDTO();
		
		medicineReturnDTO.setMedicine(medicineService.getMedicineById(medicineRequestDTO.getMedicineId()));
		medicineReturnDTO.setCategoryList(categoryService.listCategory(new Category()));
		medicineReturnDTO.setReturnView(PmsConstant.editMedicine_View);
		
		logger.info("End editMedicine");
		return medicineReturnDTO;
	}
	
	public MedicineReturnDTO viewMedicine(MedicineRequestDTO medicineRequestDTO) {
		logger.info("Start viewMedicine");
		MedicineReturnDTO medicineReturnDTO = new MedicineReturnDTO();
		
		medicineReturnDTO.setMedicine(medicineService.getMedicineById(medicineRequestDTO.getMedicineId()));
		medicineReturnDTO.setCategoryList(categoryService.listCategory(new Category()));
		medicineReturnDTO.setReturnView(PmsConstant.viewMedicine_View);
		
		logger.info("End viewMedicine");
		return medicineReturnDTO;
	}
	
	public MedicineReturnDTO createMedicine(MedicineRequestDTO medicineRequestDTO) {
		logger.info("Start createMedicine");
		MedicineReturnDTO medicineReturnDTO = new MedicineReturnDTO();
		
		medicineReturnDTO.setMedicine(new Medicine());
		medicineReturnDTO.setCategoryList(categoryService.listCategory(new Category()));
		medicineReturnDTO.setReturnView(PmsConstant.createMedicine_View);
		
		logger.info("End createMedicine");
		return medicineReturnDTO;
	}
	
	public MedicineReturnDTO saveMedicine(MedicineRequestDTO medicineRequestDTO) {
		logger.info("Start saveMedicine");
		MedicineReturnDTO medicineReturnDTO = new MedicineReturnDTO();
		
		logger.debug("Check if medicine name existed");
		Medicine dirtyMedicine = medicineRequestDTO.getDirtyMedicine();
		Medicine existedMedicine = medicineService.getMedicineByName(dirtyMedicine.getName());
		
		logger.debug("Medicine name: " + dirtyMedicine.getName());
		logger.debug("Id: " + String.valueOf(dirtyMedicine.getId()));
		if((existedMedicine != null && dirtyMedicine.getId() == null) || (existedMedicine != null && existedMedicine.getId().compareTo(dirtyMedicine.getId()) != 0)) {
			medicineReturnDTO.setReturnView(PmsConstant.editMedicine_View);
			medicineReturnDTO.setMedicine(dirtyMedicine);
			medicineReturnDTO.setErrorMessage(PmsConstant.medicineName_Existed_Key);
		} else {
			if(existedMedicine != null) {
				dirtyMedicine.setMedicineCategories(existedMedicine.getMedicineCategories());
				dirtyMedicine.getMedicineCategories().clear();
			}
			dirtyMedicine = medicineService.saveOrUpdateMedicine(dirtyMedicine);
			
			//Update medicine category
			String categoryId_mainSubCategoryId = "";
			if(medicineRequestDTO.getMainSubCategories() != null && medicineRequestDTO.getMainSubCategories().length > 0)
				categoryId_mainSubCategoryId = medicineRequestDTO.getMainSubCategories()[0];
			
			for(String categoryId : medicineRequestDTO.getCategories()) {
				for(String categoryId_subCategoryId : medicineRequestDTO.getSubCategories()) {
					if(categoryId_subCategoryId.startsWith(categoryId + "_")) {
						String subCategoryId = categoryId_subCategoryId.split("_")[1];
						MedicineCategory medicineCategory = new MedicineCategory();
						medicineCategory.setCategory(categoryService.getCategoryById(Long.valueOf(categoryId)));
						medicineCategory.setSubCategory(subCategoryService.getSubCategoryById(Long.valueOf(subCategoryId)));
						medicineCategory.setMedicine(dirtyMedicine);
						if(categoryId_mainSubCategoryId.equals(categoryId_subCategoryId))
							medicineCategory.setMainCategory(PmsConstant.Yes);
						else
							medicineCategory.setMainCategory(PmsConstant.No);
						medicineCategoryService.saveOrUpdateMedicineCategory(medicineCategory);
					}
				}
			}
			medicineReturnDTO.setReturnView(PmsConstant.listMedicine_View);
		}
		logger.info("End saveMedicine");
		return medicineReturnDTO;
	}
	
	public void deleteMedicines(String selectedMedicineIds) {
		logger.info("Start deleteMedicines");
		if(selectedMedicineIds != null && !selectedMedicineIds.isEmpty()) {
			String[] medicineIds = selectedMedicineIds.split(",");
			for(int i = 0; i < medicineIds.length; i++) {
				Long id = Long.valueOf(medicineIds[i]);
				Medicine medicine = medicineService.getMedicineById(id);
				medicineService.deleteMedicine(medicine);
			}
		}
		logger.info("End deleteMedicines");
	}
}
